<?php
namespace app\shop\controller\admin;

use app\admin\library\AdminController;
use app\admin\library\User as adminUser;

class Cate extends AdminController{
    public function onIndex(){
        $this->power('shop.cate');
        $this->pageTitle = '分类管理';
        $this->pageIcon = 'fa-navicon';
        return $this->display();
    }


    public function onTree(){
        $this->power('shop.cate');

        $Cate = $this->model('Cate');
        
        $list = $Cate->getTreeData(0);
        return ['tree' => $list];
    }

    public function onEdit(){
        $this->power('shop.cate');
        
        $id = $this->get('id',$this->check('ID')->type('int'));
    
        $this->pageTitle = '添加分类';

        if ($id){
            $this->pageTitle = '编辑分类';
            $Cate = $this->model('Cate');
            $data = $Cate->getId($id);

            try{
                $param = json_decode($data['param'],true);
            }catch(\Exception $e){
                $param = [];
            }

            $this->set('Cate',$data);
            $this->set('param',$param);
        }

        return $this->display();
    }

  

    public function onSave(){
        $this->power('shop.cate');
        
        $Cate_id = $this->post('cate_id',$this->check('ID')->type('int'));
        $data['title'] =  $this->post('title',$this->check('title')->type('string')->required());
        $data['parent_id'] =  $this->post('parent_id',$this->check()->type('int')->required());
        $data['url'] = $this->post('url',$this->check()->type('string'));
        $data['cate_model_id'] = $this->post('cate_model_id',$this->check()->type('int'));
        $data['goods_model_id'] = $this->post('goods_model_id',$this->check()->type('int'));
        $data['goods_spec_id'] = $this->post('goods_spec_id',$this->check()->type('int'));
       
        $data['param'] = json_encode($this->post('param',[]));
        
        $Cate = $this->model('Cate');
        
        if ($Cate_id){


            $oldData = $Cate->getId($Cate_id);

            $childrenList = $Cate->getKeyCollect($oldData['cate_id']);

            if (in_array($data['parent_id'],$childrenList)){
                throw new \Exception('不能选择子分类作为父分类');
            }
            
            if ($Cate->updateId($Cate_id,$data)){
                $this->log('编辑分类', ['cate_id'=>$Cate_id,'title'=>$data['title']]);
                return true;
            }else{
                throw new \Exception('编辑分类失败');
            }

        }else{
            $data['sort'] =  $Cate->where('parent_id',$data['parent_id'])->max('sort') + 1;
            
            if ($Cate_id = $Cate->insert($data)){

                $this->log('添加分类', ['Cate_id'=>$Cate_id,'title'=>$data['title']]);
                return true;
            }else{
                throw new \Exception('添加分类失败');
            }
        }
    }

    public function onDel(){
        $this->power('shop.cate');
        
        $id = $this->post('cate_id',$this->check('ID')->type('int')->required());
        

        $Cate = $this->model('Cate');


        if ($Cate->where('parent_id',$id)->count()>0){
            throw new \Exception('包含子分类，不能删除');
        }

        $Cate->clear();

        $goods = $this->model('Goods');

        if ($goods->where('cate_id',$id)->count()>0){
            throw new \Exception('包含商品，不能删除');
        }

        if ($Cate->deleteId($id)){
            $this->log('删除分类', ['cate_id'=>$id]);
            $Cate->clearTreeCache();
            return true;
       }else{
            throw new \Exception('删除分类失败');
       }
    }


    public function onSort(){
        $ids = $this->post('ids',$this->check()->type('array')->required());

        if (count($ids)<2){
            throw new \Exception('排序失败');
        }

        $Cate = $this->model('Cate');

        $Cate->switchSort($ids,'sort','asc');

        return true;
    }
}